package com.zxl.live.screen.call.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.liulishuo.filedownloader.BaseDownloadTask;
import com.zxl.live.screen.R;
import com.zxl.live.screen.call.helper.PreviewAnimHelper;
import com.zxl.live.screen.call.helper.ThemeHelper;
import com.zxl.live.screen.call.model.bean.ThemeBean;
import com.zxl.live.screen.call.ui.widget.InCallActionView;
import com.zxl.live.screen.call.ui.widget.ThemePreviewWindow;
import com.zxl.live.screen.model.TasksManager;
import com.zxl.live.screen.model.bean.DownloadBean;
import com.zxl.live.tools.base.BaseAppCompatFragment;
import com.zxl.live.tools.tools.DrawUtils;
import com.zxl.live.tools.tools.FileUtil;
import com.zxl.live.tools.tools.LogUtils;
import com.zxl.live.tools.track.StatisticsConstant;
import com.zxl.live.tools.track.StatisticsType;

/**
 * Created by Administrator on 2017/12/10.
 */

public class ThemePreviewFragment extends BaseAppCompatFragment implements View.OnClickListener, TasksManager.OnDownloadStatusListener {

    private boolean isInit;

    private ThemeBean themeBean;

    private InCallActionView actionView;

    private ThemePreviewWindow previewWindow;

    private ProgressBar mProgressBar;

    private TextView mProgressTextView;

    private View mProgressTextLayout;

    private Button mApplyBtn;

    private PreviewAnimHelper mPreviewAnimHelper;

    private OnAnimListener mOnAnimListener;

    public void setThemeBean(ThemeBean themeBean) {
        this.themeBean = themeBean;
    }

    public void setmOnAnimListener(OnAnimListener mOnAnimListener) {
        this.mOnAnimListener = mOnAnimListener;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.adapter_theme_preview, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TasksManager.getInstance().addOnDownloadStatusListener(this);
        view.setOnClickListener(this);

        mApplyBtn = (Button) view.findViewById(R.id.theme_apply_btn);
        mApplyBtn.setOnClickListener(this);

        mProgressBar = (ProgressBar) view.findViewById(R.id.theme_progress_bar);
        mProgressTextView = (TextView) view.findViewById(R.id.theme_progress_txt);
        mProgressTextLayout = view.findViewById(R.id.theme_progress_txt_holder);

        previewWindow = (ThemePreviewWindow) view.findViewById(R.id.card_flash_preview_window);
        previewWindow.refreshTheme(themeBean, DrawUtils.getScreenWidth(), DrawUtils.getScreenHeight());

        actionView = (InCallActionView) view.findViewById(R.id.card_in_call_action_view);
        actionView.refreshTheme(themeBean);

        mPreviewAnimHelper = new PreviewAnimHelper(mApplyBtn.getTranslationY());

        if (themeBean.getDownloadPath().equals(ThemeHelper.flashPath)) {
            mApplyBtn.setText(R.string.current_theme);
        } else {
            mApplyBtn.setText(R.string.apply);
        }

        isInit = true;
        if (getUserVisibleHint()) {
            loadData(true);
            LogUtils.d("create loadData : " + themeBean.title);
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isInit) {
            if (isVisibleToUser) {
               loadData(false);
                if (themeBean.getDownloadPath().equals(ThemeHelper.flashPath)) {
                    mApplyBtn.setText(R.string.current_theme);
                } else {
                    mApplyBtn.setText(R.string.apply);
                }
                LogUtils.d("setUserVisibleHint loadData : " + themeBean.title);
            } else {
                previewWindow.stop();
                actionView.setAutoRun(false);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        TasksManager.getInstance().removeOnDownloadStatusListener(this);
    }

    private void loadData(boolean isCreateFragment) {
        boolean exist = FileUtil.isFileExist(themeBean.getDownloadPath());
        actionView.setAutoRun(exist);
        if (!exist) {
            mProgressBar.setProgress(0);
            mProgressTextView.setText("0");
            TasksManager.getInstance().addTask(DownloadBean.createCallFlashBean(themeBean.src, themeBean.getDownloadPath()));
            if (isCreateFragment) {
                mPreviewAnimHelper.showProgress(mProgressBar, mProgressTextLayout);
            }
            StatisticsType.CALL_FLASH.upload(getContext(), StatisticsConstant.Event.DOWNLOAD, themeBean.title);
        } else {
            mProgressBar.setAlpha(0);
            mProgressTextLayout.setAlpha(0);
            if (isCreateFragment) {
                mPreviewAnimHelper.startAnim(previewWindow, actionView);
                mPreviewAnimHelper.showApplyBtn(mApplyBtn);
                if (null != mOnAnimListener) {
                    mOnAnimListener.onApplyBtnShow(this);
                    mOnAnimListener.onBackShow();
                }
            } else {
                mApplyBtn.setTranslationY(0);
            }
            previewWindow.play(themeBean);
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.theme_apply_btn:
                if (!themeBean.getDownloadPath().equals(ThemeHelper.flashPath)) {
                    ThemeHelper.setCallFlashTheme(getContext(), themeBean);
                    Toast.makeText(getContext(), R.string.set_success, Toast.LENGTH_SHORT).show();
                    mApplyBtn.setText(R.string.current_theme);
                    StatisticsType.CALL_FLASH.upload(getContext(), StatisticsConstant.Event.APPLY, themeBean.title);
                }
                break;
            default:
                if (FileUtil.isFileExist(themeBean.getDownloadPath())) {
                    if (mApplyBtn.getTranslationY() == 0) {
                        mPreviewAnimHelper.dismissApplyBtn(mApplyBtn);
                        if (null != mOnAnimListener) {
                            mOnAnimListener.onBackDismiss();
                        }
                    } else {
                        mPreviewAnimHelper.showApplyBtn(mApplyBtn);
                        if (null != mOnAnimListener) {
                            mOnAnimListener.onApplyBtnShow(this);
                            mOnAnimListener.onBackShow();
                        }
                    }
                }
                break;
        }
    }

    @Override
    public void onPause(BaseDownloadTask task) {
    }

    @Override
    public void onError(BaseDownloadTask task) {
        if (task.getUrl().equals(themeBean.src)) {
            StatisticsType.CALL_FLASH.upload(getContext(), StatisticsConstant.Event.DOWNLOAD_ERROR, themeBean.title);
        }
    }

    @Override
    public void onCompleted(BaseDownloadTask task) {
        if (task.getUrl().equals(themeBean.src)) {
            mPreviewAnimHelper.showApplyBtn(mApplyBtn);
            mPreviewAnimHelper.dismissProgress(mProgressBar, mProgressTextLayout);
            previewWindow.play(themeBean);
            actionView.setAutoRun(true);
            if (null != mOnAnimListener) {
                mOnAnimListener.onApplyBtnShow(this);
            }
            StatisticsType.CALL_FLASH.upload(getContext(), StatisticsConstant.Event.DOWNLOAD_SUCCESS, themeBean.title);
        }
    }

    @Override
    public void onProgress(BaseDownloadTask task, int progress) {
        if (task.getUrl().equals(themeBean.src)) {
            mProgressBar.setProgress(progress);
            mProgressTextView.setText(getResources().getString(R.string.loading_progress, progress));
        }
    }

    public interface OnAnimListener {

        void onApplyBtnShow(ThemePreviewFragment fragment);

        void onBackShow();

        void onBackDismiss();

    }
}
